package cn.gxufe.reliable.message.api.aware;

import cn.gxufe.reliable.message.api.service.CompensableMessage;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.lang.Nullable;

/**
 * @author yingjie.rong
 **/
public class AwareBeanPostProcessor implements BeanPostProcessor {

    @Autowired
    private ApplicationContext applicationContext;

    @Nullable
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if( bean instanceof CompensableMessageAware){
            ((CompensableMessageAware) bean).setCompensableMessage(applicationContext.getBean(CompensableMessage.class));
        }
        return bean;
    }

}
